<?php

namespace Index\Controller;

use Think\Controller;
use Index\ViewModel;
use Index\Service;

/**
 * 说明：通话管理控制器
 * 作者：王楠
 * 创建日期：2017-09-15
 */
class CallController extends CommonController {

    public function videocalllist() {
	$state_enum = getenumbykey('usercalllog_state');
	$state_arr = getarrbyenum($state_enum);
	$record_enum = getenumbykey('usercalllog_isrecord');
	$record_arr = getarrbyenum($record_enum);
	$app_code = $this->app_code;
	$calltime = trim(I('ddlcalltime'));
	$state = trim(I('ddlstate'));
	$record = trim(I('ddlrecord'));
	$begintime = trim(I('txtbegintime'));
	$endtime = trim(I('txtendtime'));
	$keyword = trim(I('txtkeyword'));
	$searchtype = trim(I('hidsearchtype', '0'));
	$timetype = trim(I('hidtimetype', '0'));
	$timeiscustom = trim(I('hidtimeiscustom', '0'));
	$timetype = $timeiscustom ? '0' : $timetype;
	switch ($searchtype) {
	    case '1':
		if (!empty($state)) {
//		    if ($record == '1' && $state == '2') {
//			$record = '0';
//		    }
		}
		if ($state != '1') {
		    $calltime = '0';
		}
		break;
	    case '2':
		if (!empty($record)) {
//		    if ($state == '2' && $record == '1') {
//			$state = '0';
//		    }
		}
		break;
	    case '3':
		if (!empty($calltime)) {
		    $state = '1';
		}
		break;
	    default:
		break;
	}
	$allclass = 'btn-white';
	$weekclass = 'btn-white';
	$lastweekclass = 'btn-white';
	$monthclass = 'btn-white';
	$lastmonthclass = 'btn-white';
	$quarterclass = 'btn-white';
	if (empty($timeiscustom)) {
	    switch ($timetype) {
		case '1':
		    $weekclass = 'btn-primary';
		    break;
		case '2':
		    $lastweekclass = 'btn-primary';
		    break;
		case '3':
		    $monthclass = 'btn-primary';
		    break;
		case '4':
		    $lastmonthclass = 'btn-primary';
		    break;
		case '5':
		    $quarterclass = 'btn-primary';
		    break;
		default:
		    $allclass = 'btn-primary';
		    $begintime = '';
		    $endtime = '';
		    break;
	    }
	}

	$appcode = trim(I('ddlapp'));

	if (!empty($app_code)) {
	    $map['a.app_flag'] = $app_code;
	} else {
	    if (!empty($appcode)) {
		$map['a.app_flag'] = $appcode;
	    }
	}
	$map['a.type'] = '1';
	if (!empty($calltime)) {
	    $calltime2 = explode('-', $calltime);
	    $startpoint = $calltime2[0];
	    $endpoint = $calltime2[1];
	    if (!empty($startpoint) || !empty($endpoint)) {
		if (!empty($startpoint) && !empty($endpoint)) {
		    $map['a.call_time'] = array('between', array((int) $startpoint, (int) $endpoint + 1));
		} else if (!empty($startpoint)) {
		    $map['a.call_time'] = array('egt', (int) $startpoint);
		} else if (!empty($endpoint)) {
		    $map['a.call_time'] = array('lt', (int) $endpoint);
		}
	    }
	}
	if (!empty($state)) {
	    $map['a.state'] = $state;
	}
	if (!empty($record)) {
	    $map['a.is_record'] = $record;
	}
	if (!empty($begintime) || !empty($endtime)) {
	    if (!empty($begintime) && !empty($endtime)) {
		$map['a.ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
	    } else if (!empty($begintime)) {
		$map['a.ctime'] = array('between', array(strtotime($begintime), time() + 60));
	    } else if (!empty($endtime)) {
		$map['a.ctime'] = array('between', array(0, strtotime($endtime) + 60));
	    }
	}
	if (!empty($keyword)) {
	    $where['c.user_id'] = array('eq', $keyword);
	    $where['c.user_phone'] = array('like', '%' . $keyword . '%');
	    $where['c.user_name'] = array('like', '%' . $keyword . '%');
	    $where['e.user_id'] = array('eq', $keyword);
	    $where['e.user_phone'] = array('like', '%' . $keyword . '%');
	    $where['e.user_name'] = array('like', '%' . $keyword . '%');
	    $where['_logic'] = 'or';
	    $map['_complex'] = $where;
	}
	$model = M('chat_usercalllog');
	$total = $model->alias('a')
        ->join('left join (select user_id , user_uid , user_logo , user_name , user_sex , user_phone  from chat_userinfo) c on a.user_uid=c.user_uid')
        ->join('left join (select user_id , user_uid , user_logo , user_name , user_sex , user_phone  from chat_userinfo) e on a.user_uid_callee=e.user_uid')
		->where($map)
		->count();
	$row = '10';
	$page = new Service\PageService($total, $row);
	$fpage = $page->fpage();
	$data = $model->alias('a')
		->field('a.id AS id,a.user_uid AS user_uid,a.user_uid_callee AS user_uid_callee,a.call_time AS call_time,a.money AS money,a.state AS state,a.type AS type,a.is_record AS is_record,a.channel_key AS channel_key,a.video_url AS video_url,a.video_url_ee AS video_url_ee,a.acc_url AS acc_url,a.acc_url_ee AS acc_url_ee,a.ctime AS ctime, c.user_id as user_id, c.user_logo as user_logo, c.user_name as user_name, c.user_sex as user_sex, c.user_phone as user_phone, e.user_id as user_id2, e.user_logo as user_logo2, e.user_name as user_name2, e.user_sex as user_sex2, e.user_phone as user_phone2')
        ->join('left join (select user_id , user_uid , user_logo , user_name , user_sex , user_phone  from chat_userinfo) c on a.user_uid=c.user_uid')
        ->join('left join (select user_id , user_uid , user_logo , user_name , user_sex , user_phone  from chat_userinfo) e on a.user_uid_callee=e.user_uid')
		->where($map)
		->limit($page->listfirst, $page->listRows)
		->order('ctime desc')
		->select();
	foreach ($data as $key => $value) {
	    $data[$key]['money_text'] = (int) $data[$key]['money'];
	    $data[$key]['state_text'] = $state_enum[$data[$key]['state']];
	    $data[$key]['is_record_text'] = $record_enum[$data[$key]['is_record']];
	    $data[$key]['calltime_text'] = getuitime($data[$key]['call_time']);
	    $caller_info = $this->app_name2 . '号：' . $data[$key]['user_id'] . '<br />用户昵称：' . $data[$key]['user_name'] . '<br />手机号码：' . $data[$key]['user_phone'];
	    $data[$key]['caller_info'] = $caller_info;
	    $callee_info = $this->app_name2 . '号：' . $data[$key]['user_id2'] . '<br />用户昵称：' . $data[$key]['user_name2'] . '<br />手机号码：' . $data[$key]['user_phone2'];
	    $data[$key]['callee_info'] = $callee_info;
	    $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
	}
	$search = array('appsel' => $appcode, 'calltimesel' => $calltime, 'statesel' => $state, 'recordsel' => $record, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
	$btntimeclass = array('all' => $allclass, 'week' => $weekclass, 'lastweek' => $lastweekclass, 'month' => $monthclass, 'lastmonth' => $lastmonthclass, 'quarter' => $quarterclass);
	$this->assign(array(
	    'data' => $data,
	    'fpage' => $fpage,
	    'total' => $total,
	    'search' => $search,
	    'statearr' => $state_arr,
	    'recordarr' => $record_arr,
	    'btntimeclass' => $btntimeclass,
	    'searchtype' => $searchtype,
	    'timetype' => $timetype,
	    'timeiscustom' => $timeiscustom,
	    'app_code' => $app_code,
	    'apparr' => $this->getapplist()
	));
	$this->display();
    }

    public function videorecordlist() {
	$app_code = $this->app_code;
	$begintime = trim(I('txtbegintime'));
	$endtime = trim(I('txtendtime'));
	$keyword = trim(I('txtkeyword'));
	$map = array('_complex' => '1=1');
	$appcode = trim(I('ddlapp'));

	if (!empty($app_code)) {
	    $map['app_flag'] = $app_code;
	} else {
	    if (!empty($appcode)) {
		$map['app_flag'] = $appcode;
	    }
	}
	$map['a.type'] = '1';
	$map['a.is_record'] = '2';
	if (!empty($begintime) || !empty($endtime)) {
	    if (!empty($begintime) && !empty($endtime)) {
		$map['a.ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
	    } else if (!empty($begintime)) {
		$map['a.ctime'] = array('between', array(strtotime($begintime), time() + 60));
	    } else if (!empty($endtime)) {
		$map['a.ctime'] = array('between', array(0, strtotime($endtime) + 60));
	    }
	}
	if (!empty($keyword)) {
	    $where['c.user_id'] = array('eq', $keyword);
	    $where['c.user_phone'] = array('like', '%' . $keyword . '%');
	    $where['c.user_name'] = array('like', '%' . $keyword . '%');
	    $where['e.user_id'] = array('eq', $keyword);
	    $where['e.user_phone'] = array('like', '%' . $keyword . '%');
	    $where['e.user_name'] = array('like', '%' . $keyword . '%');
	    $where['_logic'] = 'or';
	    $map['_complex'] = $where;
	}
	$model = M('chat_usercalllog');
	$total = $model->alias('a')
		->join('left join chat_userinfo c on a.user_uid=c.user_uid')
		->join('left join chat_userinfo e on a.user_uid_callee=e.user_uid')
		->where($map)
		->count();
	$row = '10';
	$page = new Service\PageService($total, $row);
	$fpage = $page->fpage();
	$data = $model->alias('a')
		->field('a.id AS id,a.user_uid AS user_uid,a.user_uid_callee AS user_uid_callee,a.channel_key AS channel_key,a.video_url AS video_url,a.video_url_ee AS video_url_ee,a.acc_url AS acc_url,a.acc_url_ee AS acc_url_ee,a.ctime AS ctime, c.user_id as user_id, c.user_logo as user_logo, c.user_name as user_name, c.user_sex as user_sex, c.user_phone as user_phone, e.user_id as user_id2, e.user_logo as user_logo2, e.user_name as user_name2, e.user_sex as user_sex2, d.user_phone as user_phone2')
		->join('left join chat_userinfo c on a.user_uid=c.user_uid')
		->join('left join chat_userinfo e on a.user_uid_callee=e.user_uid')
		->where($map)
		->limit($page->listfirst, $page->listRows)
		->order('ctime desc')
		->select();
	foreach ($data as $key => $value) {
	    $caller_info = '视客号：' . $data[$key]['user_id'] . '<br />用户昵称：' . $data[$key]['user_name'] . '<br />手机号码：' . $data[$key]['user_phone'];
	    $data[$key]['caller_info'] = $caller_info;
	    $callee_info = '视客号：' . $data[$key]['user_id2'] . '<br />用户昵称：' . $data[$key]['user_name2'] . '<br />手机号码：' . $data[$key]['user_phone2'];
	    $data[$key]['callee_info'] = $callee_info;
	    $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
	}
	$search = array('appsel' => $appcode, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
	$this->assign(array(
	    'data' => $data,
	    'fpage' => $fpage,
	    'total' => $total,
	    'search' => $search,
	    'app_code' => $app_code,
	    'apparr' => $this->getapplist()
	));
	$this->display();
    }

}
